package com.ruoyi.system.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.MeetingRoomOrder;
import com.ruoyi.system.service.IMeetingRoomOrderService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.vo.MeetingRoomVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MeetingRoom;
import com.ruoyi.system.service.IMeetingRoomService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会议室信息Controller
 *
 * @author ruoyi
 * @date 2022-04-19
 */
@RestController
@RequestMapping("/system/room")
public class MeetingRoomController extends BaseController {
    @Autowired
    private IMeetingRoomService meetingRoomService;

    @Autowired
    private IMeetingRoomOrderService meetingRoomOrderService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询会议室信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:room:list')")
    @GetMapping("/list")
    public TableDataInfo list(MeetingRoomVo meetingRoomVo) {
        startPage();
        MeetingRoom param = new MeetingRoom();
        BeanUtils.copyProperties(meetingRoomVo, param);
        List<MeetingRoom> list = meetingRoomService.selectMeetingRoomList(param);
        List<MeetingRoomVo> collect = list.stream().map(e -> {
            MeetingRoomVo vo = new MeetingRoomVo();
            BeanUtils.copyProperties(e, vo);
            MeetingRoomOrder orderByTimeAndId = meetingRoomOrderService.getOrderByTimeAndId(e.getRoomId(), meetingRoomVo.getTime());

            if (orderByTimeAndId != null) {
                SysUser sysUser = userService.selectUserById(orderByTimeAndId.getOrderUserId());
                if (sysUser != null) {
                    vo.setOrderUserName(sysUser.getNickName());
                }
                BeanUtils.copyProperties(orderByTimeAndId, vo);
//                vo.setStatus(orderByTimeAndId.getStatus());

            } else {
                vo.setStatus("3");
            }
            return vo;
        }).collect(Collectors.toList());
        return getDataTable(collect);
    }


    /**
     * 导出会议室信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:room:export')")
    @Log(title = "会议室信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MeetingRoom meetingRoom) {
        List<MeetingRoom> list = meetingRoomService.selectMeetingRoomList(meetingRoom);
        ExcelUtil<MeetingRoom> util = new ExcelUtil<MeetingRoom>(MeetingRoom.class);
        util.exportExcel(response, list, "会议室信息数据");
    }

    /**
     * 获取会议室信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:room:query')")
    @GetMapping(value = "/{roomId}")
    public AjaxResult getInfo(@PathVariable("roomId") Long roomId) {
        return AjaxResult.success(meetingRoomService.selectMeetingRoomByRoomId(roomId));
    }

    /**
     * 新增会议室信息
     */
//    @PreAuthorize("@ss.hasPermi('system:room:add')")
    @Log(title = "会议室信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MeetingRoom meetingRoom) {
        return toAjax(meetingRoomService.insertMeetingRoom(meetingRoom));
    }

    /**
     * 修改会议室信息
     */
//    @PreAuthorize("@ss.hasPermi('system:room:edit')")
    @Log(title = "会议室信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MeetingRoom meetingRoom) {
        return toAjax(meetingRoomService.updateMeetingRoom(meetingRoom));
    }

    /**
     * 删除会议室信息
     */
//    @PreAuthorize("@ss.hasPermi('system:room:remove')")
    @Log(title = "会议室信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roomIds}")
    public AjaxResult remove(@PathVariable Long[] roomIds) {
        return toAjax(meetingRoomService.deleteMeetingRoomByRoomIds(roomIds));
    }
}
